package cn.jt.controller;

import cn.jt.pojo.User;
import cn.jt.service.UserService;
import cn.jt.vo.PageSysResult;
import cn.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/findall")
   public List<User> getTest(){
        return userService.findAll();
    }

    /**
     * url:.user/login
     * @param user
     * @return SysResult对象
     */
    @PostMapping("/login")
    public SysResult login(@RequestBody User user){
     String token= userService.login(user);
      if(StringUtils.hasLength(token)){
          return  SysResult.success(token);
      }
      return SysResult.fail();
    }
    @GetMapping("/add")
    public void addUser(){
        User user=new User();
        user.setUsername("我是你爹");
        user.setPassword("123456");
        user.setEmail("123456");
        user.setPhone("99999");
        user.setStatus(true);
        userService.addUser(user);
    }


    @GetMapping("/list")
    public SysResult getUserList(PageSysResult pageSysResult  ){
        pageSysResult= userService.getUserList(pageSysResult);
        return SysResult.success(pageSysResult);

    }

    @DeleteMapping("/{id}")
    public SysResult deleteById(@PathVariable Integer id){
        userService.deleteById(id);
        return SysResult.success();
    }

    @PostMapping("/addUser")
    public SysResult addUser(@RequestBody User user){
        userService.addUser(user);
        return SysResult.success();
    }


    @PutMapping("/updateUser")
    public SysResult updateUser(@RequestBody User user){
        userService.updateUser(user);
        return SysResult.success();
    }

    @GetMapping("/{id}")
    public SysResult findById(@PathVariable Integer id){
       User user= userService.findById(id);
        return SysResult.success(user);
    }

    @PutMapping("/status/{id}/{status}")
    public SysResult updateStatus(@PathVariable Integer id,@PathVariable boolean status){
        userService.updateStatus(id,status);
        return SysResult.success();
    }
}
